package com.wsd.smartcarwasher.util;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class toExcel {

    public String dataList;
    public String[] excelTitle;
    public String[] field;
    public HttpServletResponse response;

    /*
     * createEasyExcel
     * 返回 HSSFWorkbook文件
     *
     */
    private HSSFWorkbook createEasyExcel() throws JSONException{
        HSSFWorkbook wb = new HSSFWorkbook();
        /*
         * Apache poi 相关
         *
         * row 行
         * cell 单元格
         *
         */
        HSSFSheet sheet = wb.createSheet();
        HSSFRow row = sheet.createRow(0);

        HSSFCell cell = null;
        HSSFRow rows = null;



        for (int i = 0; i < excelTitle.length; i++) {
            cell = row.createCell(i);
            cell.setCellValue(excelTitle[i]);
        }

        String list = dataList;
        //JSONObject obj = new JSONObject(list);
        JSONArray obj2 = new JSONArray(list);
        // obj.getJSONArray("rows");

        for (int i = 0; i < obj2.length(); i++) {
            JSONObject temp = obj2.getJSONObject(i);//new JSONObject(obj2.getString(i));
            rows = sheet.createRow(i+1);
            for (int j = 0; j < field.length ; j++) {
                cell = rows.createCell(j);
                cell.setCellValue(temp.getString(field[j]));
            }

        }
        return wb;

    }
    public void createExcel() {
        /*
         * response 响应头
         * 设置文件名称 result.xls
         */
        //HttpServletResponse response = ServletActionContext.getResponse();
        response.reset();
        response.setContentType("application/x-xls");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Content-Disposition", "attachment;filename=result.xls");

        try {
            ServletOutputStream out = response.getOutputStream();
            createEasyExcel().write(out);
            out.flush();
            out.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * @author CourageShuo
     * @param dataList 数据源-是一个String类型的Json文件
     * @param excelTitle 是一个String[]类型，表示excel表头
     */
    public toExcel(String dataList,String[] excelTitle){
        /*
         * response 响应头
         * 设置文件名称 result.xml
         */
        this.dataList = dataList;
        this.excelTitle = excelTitle;

    }
    /**
     * @author CourageShuo
     * @param dataList 数据源-是一个String类型的Json文件
     * @param excelTitle 是一个String[]类型，表示excel表头
     * @param field dataList中的field Json文件中的Key值 一定保持和excelTitle中的名字“对应”
     */
    public toExcel(String dataList,String[] excelTitle,String[] field,HttpServletResponse response) {
        this.dataList = dataList;
        this.excelTitle = excelTitle;
        this.field = field;
        this.response =response;
    }
}